<template>
    <div>
        <h1>添加</h1>
        <table class="table">
            <tbody>
                <tr>
                    <td>机构名称</td>
                    <td>
                        <input type="text" v-model="data.name">
                    </td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file" @change="upload">
                        <img :src="'https://localhost:7249'+data.img" height="100px" width="100px" alt="">
                    </td>
                </tr>
                <tr>
                    <td>服务类型</td>
                    <td>
                        <select v-model="data.storeTypeId">
                            <option value="0">请选择</option>
                            <option value="1">优服务</option>
                            <option value="2">没服务</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>营业时间</td>
                    <td>
                        <input type="date" v-model="data.dates">
                    </td>
                </tr>
                <tr>
                    <td>机构地址</td>
                    <td>
                        <input type="text" v-model="data.address">
                    </td>
                </tr>
                <tr>
                    <td>手机</td>
                    <td>
                        <input type="text" v-model="data.phone">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" v-model="data.state" value="true" name="state">true
                        <input type="radio" v-model="data.state" value="false" name="state" checked>false
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="button" value="添加" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref } from 'vue';

const upload=(m:any)=>{
    var a = m.target.files[0];
    var aa = new FormData();
    aa.append("file",a);

    axios.post("https://localhost:7249/api/UpLoad/UpFile",aa).then(res=>{
        data.value.img=res.data;
        alert("上传成功")
    })
}

const add=()=>{
    if(data.value.name==""){
        alert("姓名不能为空")
        return;
    }
    if(data.value.img==""){
        alert("图片不能为空")
        return;
    }
    if(data.value.address==""){
        alert("地址不能为空")
        return;
    }
    if(data.value.dates==""){
        alert("日期不能为空")
        return;
    }
    if(data.value.phone==""){
        alert("手机号不能为空")
        return;
    }

    axios.post("https://localhost:7249/api/Sto/Add",data.value).then(res=>{
        if(res.data>0){
            alert("添加成功")
            location.href="/ShowStore"
        }
        else{
            alert("添加失败")
        }
    })
}

const data=ref({
  "id": 0,
  "name": "",
  "img": "",
  "storeTypeId": 0,
  "dates": "2025-03-19T03:38:58.423Z",
  "address": "",
  "phone": "",
  "state": false
})
</script>

<style scoped>

</style>